package uk.ac.aber.paws.server.core;

import uk.ac.aber.paws.server.autosleep.SleepMonitor;
import uk.ac.aber.paws.server.config.ConfigManager;
import uk.ac.aber.paws.server.db.DBLogger;
import uk.ac.aber.paws.server.net.SecureServerSocketListener;
import uk.ac.aber.paws.server.stats.*;
import java.util.*;

import static uk.ac.aber.paws.server.config.Constants.*;

/**
 * @author Rob Johnson, Aberystwyth University
 * @email rob.johnson@aber.ac.uk
 * @date 21st May 2012
 * 
 * RunJob.java
 * 
 * This class is used to spawn a new thread to handle the specified job.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class RunJob extends Thread {
		
	private int id;
	private int sleepTime;
	private boolean running = true;
	private boolean sleepMonitorRunning = true;
	private Calendar timeEvent = null;
	
	public RunJob(int id, int sleepTime) {
		this.id = id;
		this.sleepTime = sleepTime*1000;
		
		this.start();
	}
	
	public RunJob(int id, int sleepTime, Calendar timeEvent) {
		this.id = id;
		this.sleepTime = sleepTime*1000;
		this.timeEvent = timeEvent;
		
		this.start();
	}
	
	public void stopSleepMonitor() {
		sleepMonitorRunning = false;
	}
	
	private boolean validateTimeEvent() {
		
		if (this.timeEvent != null) {
			
			Calendar now = Calendar.getInstance();
			
			if (now.get(Calendar.HOUR_OF_DAY) == this.timeEvent.get(Calendar.HOUR_OF_DAY) &&
				now.get(Calendar.MINUTE) == this.timeEvent.get(Calendar.MINUTE)) {
				
				return true;
			}
			else {
				return false;
			}
		}
		
		return true;
	}
	
	public void run() {
		
		try {
		
			if (id == RUNJOB_GENERATE_PSV_STATS) {
				
				while (running) {
					
					if (validateTimeEvent()) {
						GenerateLiveComputerStats.runMe();
					}
					
					sleep(this.sleepTime);
				}
			}
			
			// Start process to monitor all power profiles
			else if (id == RUNJOB_PROCESS_PROFILES) {
				
				while (sleepMonitorRunning) {
					
					// if the PAWS sleeping is turned on
					if (isPawsSleepingActive() && validateTimeEvent()) {
						SleepMonitor.processProfiles();
					}
					
					sleep(this.sleepTime);
				}
			}
			
			else if (id == RUNJOB_START_SECURESERVERSOCKET_LISTENER) {
				new SecureServerSocketListener(Integer.parseInt(ConfigManager.getProperty("app.net.secureservercommsport")));
			}
			
			else if (id == RUNJOB_START_MANAGEMENT_LISTENER) {
				new SecureServerSocketListener(Integer.parseInt(ConfigManager.getProperty("app.net.managementcommsport")));
			}
			
			else if (id == RUNJOB_CHECK_ARCHIVE_STATS) {
				
				while (running) {
					
					if (validateTimeEvent()) {
						Calendar now = Calendar.getInstance();
						Archiving.initiatePowerLogsArchiveProcess(now);
					}
					
					sleep(this.sleepTime);
				}
			}
			
			else if (id == RUNJOB_CHECK_ORGSAVINGS_REFRESH) {
				
				while (running) {
					
					if (validateTimeEvent()) {
						Archiving.initiateOrgSavingsRefresh();
					}
					
					sleep(this.sleepTime);
				}
			}
			
			else if (id == RUNJOB_UPDATE_TODAY_ARCHIVE_STATS) {
				
				while (running) {
					
					if (validateTimeEvent()) {
						Calendar now = Calendar.getInstance();
						Archiving.refreshTodayPowerEventLogs(now);
					}
					
					sleep(this.sleepTime);
				}	
			}
			
			else if (id == RUNJOB_CHECK_PURGE) {
				
				while (running) {
					
					if (validateTimeEvent()) {
						Archiving.purgeDatabase();
					}
					
					sleep(this.sleepTime);
				}
				
			}
			
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public boolean isPawsSleepingActive() {
		
		String value = (new DBLogger()).getControlValue("PawsServerActive");
		
		// if cannot connect to database, assume that paws sleeping is active
		if (value != null) {
			if (Integer.parseInt(value) == 1) {
				return true;
			}
			else {
				return false;
			}
		}
		return false;
	}
}
